#ifndef VNavItem_H
#define VNavItem_H					 

#include "VSqlTable.h"

///////////////////////////////////////////////////////////////////////////////
class VNavItem;
typedef QSharedPointer<VNavItem> VNavItemPtr;
class VNavItemView;
typedef QSharedPointer<VNavItemView> VNavItemViewPtr;
///////////////////////////////////////////////////////////////////////////////

class VNavItemType
{
public:
	enum EType
	{
		EType_null,
		EType_root,
		EType_tables,
		EType_tableInfo,
		EType_tableRecords,
		EType_forms,
		EType_form,
	};
	
public:
};

///////////////////////////////////////////////////////////////////////////////

class VNavDb
{
public:
	static const char *vNav() { return "vnav";}
	static const char *vNav_id() { return "vnav_id";}
	static const char *vNav_parentId() { return "vnav_parentId";}
	static const char *vNav_pos() { return "vnav_pos";}
	static const char *vNav_name() { return "vnav_name";}
	static const char *vNav_toTableName() { return "vnav_toTableName";}
	static const char *vNav_toRecId() { return "vnav_toRecId";}

	static void registerInDb();
};

///////////////////////////////////////////////////////////////////////////////

class VNavItemHeader
{
public:
	enum EColumn
	{
		displayName = 0,
		//eType,
/*
		id,
		parentId,
		pos,
		toTableName,
		toRecId
*/		
	};

public:
	VNavItemHeader();
	
	int columnCount() const { return m_columnToDisplayName.count();}

	QString columnToDisplayName(EColumn column) const;
	QString columnToDisplayName(int column) const { return columnToDisplayName((EColumn)column);}

private:
	typedef QHash<EColumn,QString> VColumnToDisplayName;
	VColumnToDisplayName m_columnToDisplayName;
};

///////////////////////////////////////////////////////////////////////////////

class VNavItem : public QObject
{
	Q_OBJECT
	V_COUNTER_OBJECTS(VNavItem)
	
public:
	static VNavItemHeader header;
	
public:
	VNavItem();
	~VNavItem();

	//void clearData();
	
	int childCount() const;
	int columnCount() const;
	int childRow() const;

	VNavItem *parent() { return m_parentItem;}
	VNavItem *child(int row) { return m_childItems[row].data();}

	bool insertChild(int row,VNavItem *insItem);
	void appendChild(VNavItem *insItem);
	//!!!
	//void appendChild(const QList<VNavItemPtr> &insItems);
	void appendChild(const QList<VNavItem*> &insItems);
	
	//bool insertChildren(int row,int count);
	bool removeChildren(int row,int count);
	void removeChildren();
	//void removeChildrenLater();
	//QList<VNavItem*> takeChildren();

	QVariant data(int column) const;
/*
	bool setData(int column,const QVariant &value);

	void setEType(VNavItemType::EType v) { setData(VNavItemHeader::eType,v);}
	VNavItemType::EType eType() const;
*/

	//void setToTableName(const QString &v) { setData(VNavItemHeader::toTableName,v);}
	//QString toTableName() const { return data(VNavItemHeader::toTableName).toString();}

	virtual VNavItemType::EType eType() const = 0;
	virtual QString displayName() const = 0;
	
	virtual bool canRemoveChildrenAsCollapsed() const { return false;}
	virtual bool canFetchMore() const { return false;}
	//!!!
	//virtual QList<VNavItemPtr> fetchMore() const { return QList<VNavItemPtr>();}
	virtual QList<VNavItem*> fetchMore() { return QList<VNavItem*>();}

	virtual VNavItemViewPtr factoryItemView(QWidget *parent) const;
	
private:
	VNavItem *m_parentItem;
	QList<VNavItemPtr> m_childItems;
};

///////////////////////////////////////////////////////////////////////////////

class VNavItemModel : public QAbstractItemModel
{
	Q_OBJECT

public:
	VNavItemModel(QObject *parent);
	~VNavItemModel();

	void setRootItem(VNavItem *item);
	
public: //QAbstractItemModel
	//bool setHeaderData(int section,Qt::Orientation orientation,const QVariant &value,int role = Qt::EditRole);
	QVariant headerData(int section,Qt::Orientation orientation,int role = Qt::DisplayRole) const;

	QModelIndex index(int row,int column,const QModelIndex &parent = QModelIndex()) const;
	QModelIndex parent(const QModelIndex &index) const;
	Qt::ItemFlags flags(const QModelIndex &index) const;

	int rowCount(const QModelIndex &parent = QModelIndex()) const;
	int columnCount(const QModelIndex &parent = QModelIndex()) const;

	bool setData(const QModelIndex &index,const QVariant &value,int role = Qt::EditRole);
	QVariant data(const QModelIndex &index,int role) const;

	//bool insertRows(int row,int count,const QModelIndex &parent = QModelIndex());
	bool removeRows(int row,int count,const QModelIndex &parent = QModelIndex());

	bool canFetchMore(const QModelIndex &parent) const;
	void fetchMore(const QModelIndex &parent);
	bool hasChildren(const QModelIndex &parent = QModelIndex()) const;
		
public:
	VNavItem *rootItem() const { return m_rootItem;}
	QModelIndex rootIndex() const { return createIndex(0,0,m_rootItem);}
	VNavItem *itemFromIndex(const QModelIndex &index) const;
	
	QModelIndex insertItem(int row,VNavItem *insItem,const QModelIndex &parent = QModelIndex());
	QModelIndex appendItem(VNavItem *insItem,const QModelIndex &parent = QModelIndex());
	void removeChildren(const QModelIndex &parent);
	//void removeChildrenLater(const QModelIndex &parent);
	//QList<VNavItem*> takeChildren(const QModelIndex &parent);
	
private:
	VNavItem *m_rootItem;
};

///////////////////////////////////////////////////////////////////////////////

class VNavSelectionModel : public QItemSelectionModel
{
	Q_OBJECT
	
public:
	VNavSelectionModel(VNavItemModel *model,QObject *parent);

	VNavItemModel *model() const;
	VNavItem *currentItem() const;
	
signals:
	void currentItemChanged(VNavItem *current);
	
private slots:
	void slot_currentRowChanged(const QModelIndex &current,const QModelIndex &previous);
};

///////////////////////////////////////////////////////////////////////////////

class VNavItemView : public QFrame//QScrollArea
{
	Q_OBJECT

public:
	VNavItemView(QWidget *parent = NULL);

	virtual VNavItemType::EType eType() const { return VNavItemType::EType_null;}

	virtual void setItemModel(VNavItemModel *itemModel)	{}
	virtual void setItem(VNavItem *item,const QModelIndex &index) {}
	virtual bool canClose() { return true;}
	
	virtual void addToolBars(QList<QToolBar*> &toolBars) {}
	//virtual void removeToolBars(QList<QToolBar*> &toolBars) {}
};

///////////////////////////////////////////////////////////////////////////////

#endif
